package com.estate.generator.controller;


import com.google.gson.Gson;
import com.estate.common.core.domain.AjaxResult;
import com.estate.common.utils.StringUtils;
import com.estate.common.utils.redis.RedisUtil;
import com.estate.generator.service.QiniuService;
import com.estate.system.domain.*;
import com.estate.system.service.*;
import com.estate.system.service.impl.LentServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Controller
public class FileUploadController {
    @Resource
    private QiniuService qiniuService;
    @Resource
    private ILentService lentService;
    @Resource
    private IBuyService buyService;
    @Resource
    private ISecondHandService secondHandService;
    @Resource
    private IIntroductionService iIntroductionService;
    @Resource
    private IUrgentHouseService urgentHouseService;
    @Resource
    private IResidentialService residentialService;
    @Resource
    private IApartmentService apartmentService;
    @Resource
    private ICarouselService carouselService;
    @Resource
    private RedisUtil redisUtil;
    Gson gson = new Gson();
    
    @RequestMapping("/hello")
    @ResponseBody
    public String hello(){
        return "upload";
    }
    @RequestMapping(value = "/testUpload", method = RequestMethod.POST)
    @ResponseBody
    public FileRes uploadImage(@RequestPart("file") MultipartFile file,@RequestParam int uploadId) {
        System.out.println(uploadId+"extra");
        if(file.isEmpty()) {
            FileRes fileRes = new FileRes();
            fileRes.setError("You are not allowed to upload such a file.");
            return fileRes;
        }
        try {
            String fileUrl=qiniuService.saveImageByName(file,"buy").trim();
            String subStr=fileUrl.replaceFirst("http://","//");
            System.out.println("mystr"+subStr);
            System.out.println("qiniuService.saveImageByName(file,\"buy\")"+fileUrl);
            Buy buy = new Buy();
            buy.setId((long) uploadId);
            buy.setImgAddress(subStr);
            Buy buy1 = buyService.selectBuyById((long) uploadId);
            if (buy1!=null){
                buyService.updateBuy(buy);

                //设置 key:原来buy的对象  value:新更新的对象
                //由于知道id,直接 buy:1格式
                redisUtil.del("buy:"+uploadId);
                redisUtil.del("allBuy:");

            }else{
                Buy buy2 = new Buy();
                buy2.setId((long) uploadId);
                buy2.setImgAddress(fileUrl);
                int i = buyService.insertBuy(buy2);
                if (i>0){

                    redisUtil.del("buy:"+uploadId);
                }

            }


            FileRes fileRes = new FileRes();
            return fileRes;
        } catch (IOException e) {
            e.printStackTrace();
        }
        FileRes fileRes = new FileRes();
        return fileRes;
    }


    @RequestMapping(value = "/lentTestUpload", method = RequestMethod.POST)
    @ResponseBody
    public FileRes lentUploadImage(@RequestPart("file") MultipartFile file,@RequestParam int uploadId) {
        System.out.println(uploadId+"extra");
        if(file.isEmpty()) {
            FileRes fileRes = new FileRes();
            fileRes.setError("You are not allowed to upload such a file.");
            return fileRes;
        }
        try {
            String fileUrl=qiniuService.saveImageByName(file,"lent");
            String subStr=fileUrl.replaceFirst("http://","//");

            Lent lent = new Lent();
            lent.setId((long) uploadId);
            lent.setImgAddress(subStr);
            lentService.updateLent(lent);

            FileRes fileRes = new FileRes();
            return fileRes;
        } catch (IOException e) {
            e.printStackTrace();
        }
        FileRes fileRes = new FileRes();
        return fileRes;
    }
    @RequestMapping(value = "/carouselTestUpload", method = RequestMethod.POST)
    @ResponseBody
    public FileRes carouselUploadImage(@RequestPart("file") MultipartFile file,@RequestParam int uploadId) {
        System.out.println(uploadId+"extra");
        if(file.isEmpty()) {
            FileRes fileRes = new FileRes();
            fileRes.setError("You are not allowed to upload such a file.");
            return fileRes;
        }
        try {
            String fileUrl=qiniuService.saveImageByName(file,"carousel");
            String subStr=fileUrl.replaceFirst("http://","//");

            Carousel carousel = new Carousel();
            carousel.setId((long) uploadId);
            carousel.setImgAddress(subStr);
            carouselService.updateCarousel(carousel);

            FileRes fileRes = new FileRes();
            return fileRes;
        } catch (IOException e) {
            e.printStackTrace();
        }
        FileRes fileRes = new FileRes();
        return fileRes;
    }
  
    /*
     * 上传secondHand的首页图片
     * */
    @RequestMapping(value = "/secondHandTestUpload", method = RequestMethod.POST)
    @ResponseBody
    public FileRes uploadImageSecondHand(@RequestPart("file") MultipartFile file,@RequestParam int uploadId) {
        System.out.println(uploadId+"extra");
        if(file.isEmpty()) {
            FileRes fileRes = new FileRes();
            fileRes.setError("You are not allowed to upload such a file.");
            return fileRes;
        }
        try {
            String fileUrl=qiniuService.saveImageByName(file,"secondHand");
            String subStr=fileUrl.replaceFirst("http://","//");

            SecondHand secondHand = new SecondHand();
            secondHand.setId((long) uploadId);
            secondHand.setImgAddress(subStr);
            secondHandService.updateSecondHand(secondHand);

            FileRes fileRes = new FileRes();
            return fileRes;
        } catch (IOException e) {
            e.printStackTrace();
        }
        FileRes fileRes = new FileRes();
        return fileRes;
    }
    /*
     * 上传Introduction的首页图片
     * */
    @RequestMapping(value = "/introductionTestUpload", method = RequestMethod.POST)
    @ResponseBody
    public FileRes uploadImageIntroduction(@RequestPart("file") MultipartFile file,@RequestParam int uploadId) {
        System.out.println(uploadId+"extra");
        if(file.isEmpty()) {
            FileRes fileRes = new FileRes();
            fileRes.setError("You are not allowed to upload such a file.");
            return fileRes;
        }
        try {
            String fileUrl=qiniuService.saveImageByName(file,"introduction");
            String subStr=fileUrl.replaceFirst("http://","//");

            Introduction introduction = new Introduction();
            introduction.setId((long) uploadId);
            introduction.setImgAddress(subStr);
            iIntroductionService.updateIntroduction(introduction);

            FileRes fileRes = new FileRes();
            return fileRes;
        } catch (IOException e) {
            e.printStackTrace();
        }
        FileRes fileRes = new FileRes();
        return fileRes;
    }

    /*
     * 上传UrgentHouse的首页图片
     * */
    @RequestMapping(value = "/urgentHouseTestUpload", method = RequestMethod.POST)
    @ResponseBody
    public FileRes uploadImageUrgentHouse(@RequestPart("file") MultipartFile file,@RequestParam int uploadId) {
        System.out.println(uploadId+"extra");
        if(file.isEmpty()) {
            FileRes fileRes = new FileRes();
            fileRes.setError("You are not allowed to upload such a file.");
            return fileRes;
        }
        try {
            String fileUrl=qiniuService.saveImageByName(file,"urgentHouse");
            String subStr=fileUrl.replaceFirst("http://","//");

            UrgentHouse urgentHouse = new UrgentHouse();
            urgentHouse.setId((long) uploadId);
            urgentHouse.setImgAddress(subStr);
            urgentHouseService.updateUrgentHouse(urgentHouse);

            FileRes fileRes = new FileRes();
            return fileRes;
        } catch (IOException e) {
            e.printStackTrace();
        }
        FileRes fileRes = new FileRes();
        return fileRes;
    }
    /*
     * 上传Apartment的首页图片
     * */
    @RequestMapping(value = "/residentialTestUpload", method = RequestMethod.POST)
    @ResponseBody
    public FileRes residentialTestUpload(@RequestPart("file") MultipartFile file,@RequestParam int uploadId) {
        System.out.println(uploadId+"extra");
        if(file.isEmpty()) {
            FileRes fileRes = new FileRes();
            fileRes.setError("You are not allowed to upload such a file.");
            return fileRes;
        }
        try {
            String fileUrl=qiniuService.saveImageByName(file,"residential");
            String subStr=fileUrl.replaceFirst("http://","//");
             Residential residential= new Residential();
            residential.setId((long) uploadId);
            residential.setImgAddress(subStr);
            residentialService.updateResidential(residential);

            FileRes fileRes = new FileRes();
            return fileRes;
        } catch (IOException e) {
            e.printStackTrace();
        }
        FileRes fileRes = new FileRes();
        return fileRes;
    }

    /*
     * 上传Apartment的首页图片
     * */
    @RequestMapping(value = "/apartmentTestUpload", method = RequestMethod.POST)
    @ResponseBody
    public FileRes testUploadApartment(@RequestPart("file") MultipartFile file,@RequestParam int uploadId) {
        System.out.println(uploadId+"extra");
        if(file.isEmpty()) {
            FileRes fileRes = new FileRes();
            fileRes.setError("You are not allowed to upload such a file.");
            return fileRes;
        }
        try {
            String fileUrl=qiniuService.saveImageByName(file,"apartment");
            String subStr=fileUrl.replaceFirst("http://","//");
            Apartment apartment = new Apartment();
            apartment.setId((long) uploadId);
            apartment.setImgAddress(subStr);
            apartmentService.updateApartment(apartment);

            FileRes fileRes = new FileRes();
            return fileRes;
        } catch (IOException e) {
            e.printStackTrace();
        }
        FileRes fileRes = new FileRes();
        return fileRes;
    }
    /*
     * 上传Apartment的首页图片
     * */
    @RequestMapping(value = "/testUploadUrgentHouse", method = RequestMethod.POST)
    @ResponseBody
    public FileRes testUploadUrgentHouse(@RequestPart("file") MultipartFile file,@RequestParam int uploadId) {
        System.out.println(uploadId+"extra");
        if(file.isEmpty()) {
            FileRes fileRes = new FileRes();
            fileRes.setError("You are not allowed to upload such a file.");
            return fileRes;
        }
        try {
            String fileUrl=qiniuService.saveImageByName(file,"urgentHouse");
            String subStr=fileUrl.replaceFirst("http://","//");
            UrgentHouse urgentHouse = new UrgentHouse();
            urgentHouse.setId((long) uploadId);
            urgentHouse.setImgAddress(subStr);
            urgentHouseService.updateUrgentHouse(urgentHouse);

            FileRes fileRes = new FileRes();
            return fileRes;
        } catch (IOException e) {
            e.printStackTrace();
        }
        FileRes fileRes = new FileRes();
        return fileRes;
    }





    /**
     *  删除buyHead图片
     */
    @PostMapping("/deleteBuyHeadImage")
    @ResponseBody
    public AjaxResult deleteImage(String fileName,int uploadId){
        System.out.println("fileName"+fileName);
        boolean res = qiniuService.deletePhoto(fileName,"buy");
        System.out.println(res);

        Buy buy = new Buy();
        buy.setId((long) uploadId);
        //标记已经删除
        buy.setImgAddress("deleted");
        int uploadRes = buyService.updateBuy(buy);
        System.out.println("uploadRes"+uploadRes);
        if(uploadRes!=0 && res==true){
            //由于无法直接得到原来的值,用redis获取

            redisUtil.del("buy:"+uploadId);

            return  new AjaxResult().put("success","删除成功");
        }else{
            return  new AjaxResult().put("error","删除失败");
        }

    }

    /**
     *  删除buyHead图片
     */
    @PostMapping("/deleteLentHeadImage")
    @ResponseBody
    public AjaxResult deleteLentImage(String fileName,int uploadId){
        System.out.println("fileName"+fileName);
        boolean res = qiniuService.deletePhoto(fileName,"lent");
        System.out.println(res);

        Lent lent = new Lent();
        lent.setId((long) uploadId);
        //标记已经删除
        lent.setImgAddress("deleted");
        int uploadRes = lentService.updateLent(lent);
        System.out.println("uploadRes"+uploadRes);
        if(uploadRes!=0 && res==true){
            return  new AjaxResult().put("success","删除成功");
        }else{
            return  new AjaxResult().put("error","删除失败");
        }

    }
    /**
     *  删除buyHead图片
     */
    @PostMapping("/deleteCarouselHeadImage")
    @ResponseBody
    public AjaxResult deleteCarouselImage(String fileName,int uploadId){
        System.out.println("fileName"+fileName);
        boolean res = qiniuService.deletePhoto(fileName,"carousel");
        System.out.println(res);

        Carousel carousel = new Carousel();
        carousel.setId((long) uploadId);
        //标记已经删除
        carousel.setImgAddress("deleted");
        int uploadRes = carouselService.updateCarousel(carousel);
        System.out.println("uploadRes"+uploadRes);
        if(uploadRes!=0 && res==true){
            return  new AjaxResult().put("success","删除成功");
        }else{
            return  new AjaxResult().put("error","删除失败");
        }

    }


    @PostMapping("/deleteSecondHandHeadImage")
    @ResponseBody
    public AjaxResult deleteSecondHandImage(String fileName,int uploadId){
        System.out.println("fileName"+fileName);
        boolean res = qiniuService.deletePhoto(fileName,"secondHand");
        System.out.println(res);

        SecondHand secondHand = new SecondHand();
        secondHand.setId((long) uploadId);
        //标记已经删除
        secondHand.setImgAddress("deleted");
        int uploadRes = secondHandService.updateSecondHand(secondHand);
        System.out.println("uploadRes"+uploadRes);
        if(uploadRes!=0 && res==true){
            return  new AjaxResult().put("success","删除成功");
        }else{
            return  new AjaxResult().put("error","删除失败");
        }

    }
    @PostMapping("/introduction/deleteIntroductionHeadImage")
    @ResponseBody
    public AjaxResult deleteIntroductionHeadImage(String fileName,int uploadId){
        System.out.println("fileName"+fileName);
        boolean res = qiniuService.deletePhoto(fileName,"introduction");
        System.out.println(res);

        Introduction introduction = new Introduction();
        introduction.setId((long) uploadId);
        //标记已经删除
        introduction.setImgAddress("deleted");
        int uploadRes = iIntroductionService.updateIntroduction(introduction);
        System.out.println("uploadRes"+uploadRes);
        if(uploadRes!=0 && res==true){
            return  new AjaxResult().put("success","删除成功");
        }else{
            return  new AjaxResult().put("error","删除失败");
        }

    }
    @PostMapping("/urgentHouse/deleteUrgentHouseHeadImage")
    @ResponseBody
    public AjaxResult deleteUrgentHouseHeadImage(String fileName,int uploadId){
        System.out.println("fileName"+fileName);
        boolean res = qiniuService.deletePhoto(fileName,"urgentHouse");
        System.out.println(res);

        UrgentHouse urgentHouse = new UrgentHouse();
        urgentHouse.setId((long) uploadId);
        //标记已经删除
        urgentHouse.setImgAddress("deleted");
        int uploadRes = urgentHouseService.updateUrgentHouse(urgentHouse);
        System.out.println("uploadRes"+uploadRes);
        if(uploadRes!=0 && res==true){
            return  new AjaxResult().put("success","删除成功");
        }else{
            return  new AjaxResult().put("error","删除失败");
        }

    }
    @PostMapping("/residential/deleteResidentialHeadImage")
    @ResponseBody
    public AjaxResult deleteResidentialHeadImage(String fileName,int uploadId){
        System.out.println("fileName"+fileName);
        boolean res = qiniuService.deletePhoto(fileName,"residential");
        System.out.println(res);

        Residential residential = new Residential();
        residential.setId((long) uploadId);
        //标记已经删除
        residential.setImgAddress("deleted");
        int uploadRes = residentialService.updateResidential(residential);
        System.out.println("uploadRes"+uploadRes);
        if(uploadRes!=0 && res==true){
            return  new AjaxResult().put("success","删除成功");
        }else{
            return  new AjaxResult().put("error","删除失败");
        }

    }
    @PostMapping("/apartment/deleteApartmentHeadImage")
    @ResponseBody
    public AjaxResult deleteApartmentHeadImage(String fileName,int uploadId){
        System.out.println("fileName"+fileName);
        boolean res = qiniuService.deletePhoto(fileName,"apartment");
        System.out.println(res);

        Apartment apartment = new Apartment();
        apartment.setId((long) uploadId);
        //标记已经删除
        apartment.setImgAddress("deleted");
        int uploadRes = apartmentService.updateApartment(apartment);
        System.out.println("uploadRes"+uploadRes);
        if(uploadRes!=0 && res==true){
            return  new AjaxResult().put("success","删除成功");
        }else{
            return  new AjaxResult().put("error","删除失败");
        }

    }

}
